package com.dao.imp;

import com.dao.FindAllChildTypeDao;
import com.dao.FindAllParentTypeDao;
import com.entity.ChildType;
import com.entity.GoodType;
import com.entity.ParentType;
import com.util.DBConn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mathison
 * @Title: FindAllType
 * @Package: com.dao.imp
 * @Description: TODO
 * @date 2021/11/21 16:18
 **/
public class FindAllType implements FindAllParentTypeDao, FindAllChildTypeDao {
    @Override
    public List<ChildType> getChildType(String id) {
        List<ChildType> childTypeList = new ArrayList<>();
        String sql = "select * from childtype where parent_typeId="+id;
//        System.out.println(sql);
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
            while (resultSet.next()){
                ChildType childType = new ChildType();
                childType.setType_name(resultSet.getString("child_type"));
                childType.setType_id(resultSet.getInt("child_typeId"));
                childType.setParent_typeId(resultSet.getInt("parent_typeId"));
                childTypeList.add(childType);
            }
//            DBConn.closeConn();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return childTypeList;
    }

    @Override
    public List<ParentType> getParentType() {
        List<ParentType> parentTypeList = new ArrayList<>();

        String sql = "SELECT * FROM parenttype";
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);

            while (resultSet.next()){
//                System.out.println(resultSet.getString("parent_type")+":"+resultSet.getInt("parent_typeId"));
                ParentType parentType = new ParentType();
                parentType.setType_name(resultSet.getString("parent_type"));
                parentType.setType_id(resultSet.getInt("parent_typeId"));
                parentTypeList.add(parentType);
            }
//            DBConn.closeConn();
        }catch (SQLException e){
            e.printStackTrace();
        }
//        for (ParentType item:
//             parentTypeList) {
//            System.out.println(item);
//        }
        return parentTypeList;
    }

    public List<GoodType> getGoodType(){
        List<GoodType> goodTypeList = new ArrayList<>();
        String sql = "SELECT * FORM goodtype";
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
            while (resultSet.next()){
                GoodType goodType = new GoodType();
                goodType.setChild_type(resultSet.getString("child_type"));
                goodType.setChild_typeId(resultSet.getInt("child_typeId"));
                goodType.setParent_type(resultSet.getString("parent_type"));
                goodType.setParent_typeId(resultSet.getInt("parent_typeId"));

                goodTypeList.add(goodType);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return goodTypeList;
    }


}
